package view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import model.User;
import dao.UserDAO;

public class EditUserDialog extends JDialog {
    private JTextField usernameField;
    private JComboBox<String> roleCombo;
    private UserPanel userPanel;
    private User user;
    
    public EditUserDialog(JFrame parent, UserPanel userPanel, User user) {
        super(parent, "编辑用户", true);
        this.userPanel = userPanel;
        this.user = user;
        
        setLayout(new GridLayout(3, 2, 5, 5));
        
        // 添加输入框
        add(new JLabel("用户名："));
        usernameField = new JTextField(user.getUsername());
        add(usernameField);
        
        add(new JLabel("角色："));
        roleCombo = new JComboBox<>(new String[]{"user", "admin"});
        roleCombo.setSelectedItem(user.getRole());
        add(roleCombo);
        
        // 添加按钮
        JButton saveBtn = new JButton("保存");
        JButton cancelBtn = new JButton("取消");
        
        saveBtn.addActionListener(e -> updateUser());
        cancelBtn.addActionListener(e -> dispose());
        
        add(saveBtn);
        add(cancelBtn);
        
        styleComponents();
        
        pack();
        setLocationRelativeTo(parent);
    }
    
    private void updateUser() {
        String username = usernameField.getText().trim();
        String role = (String) roleCombo.getSelectedItem();
        
        // 验证输入
        if (username.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // 如果是admin用户，不允许修改角色
        if ("admin".equals(user.getUsername()) && !"admin".equals(role)) {
            JOptionPane.showMessageDialog(this, "不能修改管理员角色！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        UserDAO dao = new UserDAO();
        
        // 如果修改了用户名，检查新用户名是否已存在
        if (!username.equals(user.getUsername()) && !dao.checkUsername(username)) {
            JOptionPane.showMessageDialog(this, "用户名已存在！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // 更新用户信息
        user.setUsername(username);
        user.setRole(role);
        
        if (dao.updateUser(user)) {
            JOptionPane.showMessageDialog(this, "更新成功！");
            userPanel.refreshTable();
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "更新失败！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void styleComponents() {
        // 设置字体
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 14);
        Font inputFont = new Font("微软雅黑", Font.PLAIN, 14);
        
        // 设置输入框样式
        usernameField.setFont(inputFont);
        roleCombo.setFont(inputFont);
        
        // 设置标签样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                label.setFont(labelFont);
            }
        }
        
        // 设置按钮样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JButton) {
                JButton button = (JButton) comp;
                button.setFont(labelFont);
                button.setFocusPainted(false);
                button.setBackground(new Color(59, 89, 152));
                button.setForeground(Color.WHITE);
                button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
            }
        }
        
        // 设置输入框边框
        Border border = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(204, 204, 204)),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        );
        usernameField.setBorder(border);
        roleCombo.setBorder(border);
        
        // 设置窗口大小
        setPreferredSize(new Dimension(400, 200));
    }
} 